home *** CD-ROM | disk | FTP | other *** search
- #include <conio.h>
- #include <dos.h>
- #include <mem.h>
- #include <stdio.h>
-
- #include "yprim.hpp"
-
- BYTE *RowsY[200];
- BYTE Yfont[2048];
-
-
- void
- set80x25(void)
- {
- union REGS r;
- r.x.eax = 0x0003;
-
- int386(0x10, &r, &r);
- }
-
-
- void
- set320x200x256(void)
- {
- union REGS r;
- int i;
-
- r.x.eax = 0x0013;
-
- int386(0x10, &r, &r);
-
- for (i=0; i < 200; i++) {
- RowsY[i] = (unsigned char *)((0xA000 << 4) + (320 * i));
- }
- }
-
-
- void
- wait_for_retrace(void)
- {
- while (!(inp(0x3DA) & 0x08));
- }
-
-
- void
- clear(BYTE color)
- {
- memset((unsigned char *)(0xA000 << 4), color, 64000L);
- }
-
-
- void
- putpixel(COORD x, COORD y, BYTE color)
- {
- *(RowsY[y] + x) = color;
- }
-
-
- inline void
- internal_putpixel(COORD x, COORD y, BYTE color)
- {
- *(RowsY[y] + x) = color;
- }
-
-
- BYTE
- getpixel(COORD x, COORD y)
- {
- return *(RowsY[y] + x);
- }
-
-
- inline void
- internal_vert_line(int x, int top_y, int len, BYTE color)
- {
- BYTE *ptr;
-
- ptr = RowsY[top_y] + x;
-
- while (len--) {
- *ptr = color;
- ptr += 320;
- }
- }
-
-
- inline void
- internal_horiz_line(int left_x, int y, int len, BYTE color)
- {
- memset((RowsY[y] + left_x), color, len);
- }
-
-
- void
- box(COORD x1, COORD y1, COORD x2, COORD y2, BYTE color)
- {
- int xsize, ysize;
-
- xsize = (x2 - x1) + 1;
- internal_horiz_line(x1, y1, xsize, color);
- internal_horiz_line(x1, y2, xsize, color);
-
- y1++;
- ysize = (y2 - y1);
-
- internal_vert_line(x1, y1, ysize, color);
- internal_vert_line(x2, y1, ysize, color);
- }
-
-
- void
- filledbox(COORD x1, COORD y1, COORD x2, COORD y2, BYTE color)
- {
- int xsize, ysize;
-
- xsize = (x2 - x1) + 1;
- ysize = (y2 - y1) + 1;
-
- while (ysize--) {
- internal_horiz_line(x1, y1++, xsize, color);
- }
- }
-
-
- void
- line(COORD lx1, COORD ly1, COORD lx2, COORD ly2, BYTE color)
- {
- unsigned char *ptr;
- unsigned char *ptr2;
- unsigned long ErrorAcc, ErrorAdj;
- int DeltaX, DeltaY, XDir, temp;
-
- // Line drawing with Bresenham's integer line-drawing algorithm for speed
- // Conversion to symmetrical Bresenham by David Boeren
-
- // Make sure the line runs top to bottom
- if (ly1 > ly2) {
- temp = lx1; lx1 = lx2; lx2 = temp;
- temp = ly1; ly1 = ly2; ly2 = temp;
- }
-
- DeltaX = (lx2 - lx1);
-
- if (DeltaX >= 0) {
- XDir = 1;
- } else {
- XDir = -1;
- DeltaX = -DeltaX; // Make DeltaX positive
- }
-
- DeltaY = (ly2 - ly1);
-
- if (DeltaY == 0) {
- // Horizontal Line (and one pixel lines)
- if (XDir == 1) {
- memset((RowsY[ly1] + lx1), color, (DeltaX + 1));
- } else {
- memset((RowsY[ly1] + lx2), color, (DeltaX + 1));
- }
- return;
- }
-
- if (DeltaX == 0) {
- // Vertical Line
- internal_vert_line(lx1, ly1, (DeltaY + 1), color);
- return;
- }
-
- // Draw the initial pixels
- ptr = RowsY[ly1] + lx1;
- *ptr = color;
- ptr2 = RowsY[ly2] + lx2;
- *ptr2 = color;
-
- // initialize line error accumulator to .5, so we can
- // advance when we get halfway to the next pixel
- ErrorAcc = 0x8000;
-
- // Is this an X-major or a Y-major line?
- if (DeltaY > DeltaX) {
- // Y-major line; calculate 16-bit fixed point fractional part
- // of a pixel that X advances each time Y advances 1 pixel
- ErrorAdj = ((((unsigned long)DeltaX << 17) /
- (unsigned long)DeltaY) + 1) >> 1;
-
- // Draw all pixels between the first and last
- DeltaY = (DeltaY >> 1);
- while (DeltaY--) {
- // calculate error for this pixel
- ErrorAcc += ErrorAdj;
-
- if (ErrorAcc & ~0xFFFFL) {
- // clear integer part of result
- ErrorAcc &= 0xFFFFL;
-
- // The error accumulator turned over, so advance the X coord
- ptr += XDir;
- ptr2 -= XDir;
- }
-
- ptr += 320;
- *ptr = color;
- ptr2 -= 320;
- *ptr2 = color;
- }
- } else {
- // It's an X-major line; calculate 16-bit fixed point fractional
- // part of a pixel that Y advances each time X advances 1 pixel
- ErrorAdj = ((((unsigned long)DeltaY << 17) /
- (unsigned long)DeltaX) + 1) >> 1;
-
- // Draw all remaining pixels
- DeltaX = (DeltaX >> 1);
- while (DeltaX--) {
- // calculate error for this pixel
- ErrorAcc += ErrorAdj;
-
- if (ErrorAcc & ~0xFFFFL) {
- // clear integer part of result
- ErrorAcc &= 0xFFFFL;
-
- // The error accumulator turned over, so advance the Y coord
- ptr += 320;
- ptr2 -= 320;
- }
-
- ptr += XDir;
- *ptr = color;
- ptr2 -= XDir;
- *ptr2 = color;
- }
- }
- }
-
-
- void
- circle(COORD x, COORD y, DIST r, BYTE color)
- {
- int ix, iy, d, deltaE, deltaSE;
-
- ix = 0;
- iy = r;
- d = 1 - r;
- deltaE = 3;
- deltaSE = (-2 * r) + 5;
-
- internal_putpixel(x + ix, y + iy, color);
- internal_putpixel(x + ix, y - iy, color);
- internal_putpixel(x + iy, y + ix, color);
- internal_putpixel(x + iy, y - ix, color);
- internal_putpixel(x - ix, y + iy, color);
- internal_putpixel(x - ix, y - iy, color);
- internal_putpixel(x - iy, y + ix, color);
- internal_putpixel(x - iy, y - ix, color);
-
- while (iy > ix++) {
- if (d < 0) {
- d += deltaE;
- deltaE += 2;
- deltaSE += 2;
- } else {
- d += deltaSE;
- deltaE += 2;
- deltaSE += 4;
- iy--;
- }
-
- internal_putpixel(x + ix, y + iy, color);
- internal_putpixel(x + ix, y - iy, color);
- internal_putpixel(x + iy, y + ix, color);
- internal_putpixel(x + iy, y - ix, color);
- internal_putpixel(x - ix, y + iy, color);
- internal_putpixel(x - ix, y - iy, color);
- internal_putpixel(x - iy, y + ix, color);
- internal_putpixel(x - iy, y - ix, color);
- }
- }
-
-
- void
- filledcircle(COORD x, COORD y, DIST r, BYTE color)
- {
- int ix, iy, d, deltaE, deltaSE;
-
- ix = 0;
- iy = r;
- d = 1 - r;
- deltaE = 3;
- deltaSE = (-2 * r) + 5;
-
- internal_horiz_line(x - ix, y + iy, (ix << 1) + 1, color);
- internal_horiz_line(x - ix, y - iy, (ix << 1) + 1, color);
- internal_horiz_line(x - iy, y + ix, (iy << 1) + 1, color);
- internal_horiz_line(x - iy, y - ix, (iy << 1) + 1, color);
-
- while (iy > ix++) {
- if (d < 0) {
- d += deltaE;
- deltaE += 2;
- deltaSE += 2;
- } else {
- d += deltaSE;
- deltaE += 2;
- deltaSE += 4;
- iy--;
- }
-
- internal_horiz_line(x - ix, y + iy, (ix << 1) + 1, color);
- internal_horiz_line(x - ix, y - iy, (ix << 1) + 1, color);
- internal_horiz_line(x - iy, y + ix, (iy << 1) + 1, color);
- internal_horiz_line(x - iy, y - ix, (iy << 1) + 1, color);
- }
- }
-
-
- int
- loadfont(char *fname)
- {
- FILE *fp;
-
- fp = fopen(fname, "rb");
-
- if (fp == NULL) {
- return 0;
- } else {
- fread(Yfont, 8, 256, fp);
- fclose(fp);
- return 1;
- }
- }
-
-
- void
- putch(COORD x, COORD y, char c, BYTE color)
- {
- short int i;
- BYTE temp;
- BYTE *vga_ptr;
- BYTE *font_ptr;
-
- vga_ptr = RowsY[y << 3] + (x << 3);
- font_ptr = Yfont + (c << 3);
-
- i=8;
- while (i--) {
- temp = *font_ptr++;
- if (temp & 0x01) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x02) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x04) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x08) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x10) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x20) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x40) {
- *vga_ptr = color;
- }
- vga_ptr++;
- if (temp & 0x80) {
- *vga_ptr = color;
- }
-
- vga_ptr += 313; // 320 - 7
- }
- }
-
-
- void
- putstring(COORD x, COORD y, char *str, BYTE color)
- {
- while (*str) {
- putch(x++,y,*str++,color);
- }
- }
-
-